// 修改导出方式
export const CardPattern = {
    SINGLE: 'SINGLE',          // 单张
    PAIR: 'PAIR',             // 对子
    TRIPLE: 'TRIPLE',         // 三张
    TRIPLE_WITH_ONE: 'TRIPLE_WITH_ONE', // 三带一
    TRIPLE_WITH_PAIR: 'TRIPLE_WITH_PAIR', // 三带对
    STRAIGHT: 'STRAIGHT',     // 顺子
    STRAIGHT_PAIR: 'STRAIGHT_PAIR', // 连对
    AIRPLANE: 'AIRPLANE',     // 飞机
    AIRPLANE_WITH_WINGS: 'AIRPLANE_WITH_WINGS', // 飞机带翅膀
    BOMB: 'BOMB',            // 炸弹
    ROCKET: 'ROCKET',        // 王炸
    INVALID: 'INVALID',       // 无效牌型
    FOUR_WITH_TWO: 'FOUR_WITH_TWO',         // 四带二
    FOUR_WITH_TWO_PAIRS: 'FOUR_WITH_TWO_PAIRS'  // 四带二对
};

// 牌型判断工具类
export const CardUtils = {
    // 获取牌的权重值
    getCardWeight(rank) {
        const weightMap = {
            '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10,
            'J': 11, 'Q': 12, 'K': 13, 'A': 14, '2': 15,
            'SMALL': 16, 'BIG': 17
        };
        return weightMap[rank] || 0;
    },

    // 获取牌型
    getPattern(cards) {
        if (!cards || cards.length === 0) return CardPattern.INVALID;

        // 按点数分组
        const rankGroups = {};
        cards.forEach(card => {
            rankGroups[card.rank] = (rankGroups[card.rank] || []).concat(card);
        });

        // 检查是否是王炸
        if (this.isRocket(cards)) return CardPattern.ROCKET;
        
        // 检查是否是炸弹
        if (this.isBomb(cards)) return CardPattern.BOMB;

        // 根据牌的数量判断可能的牌型
        switch (cards.length) {
            case 1: return CardPattern.SINGLE;
            case 2: return this.isPair(cards) ? CardPattern.PAIR : CardPattern.INVALID;
            case 3: return this.isTriple(cards) ? CardPattern.TRIPLE : CardPattern.INVALID;
            case 4: return this.isTripleWithOne(cards) ? CardPattern.TRIPLE_WITH_ONE : CardPattern.INVALID;
            case 5:
                if (this.isTripleWithPair(cards)) return CardPattern.TRIPLE_WITH_PAIR;
                if (this.isStraight(cards)) return CardPattern.STRAIGHT;
                return CardPattern.INVALID;
            case 6:
                if (this.isFourWithTwo(cards)) return CardPattern.FOUR_WITH_TWO;
                if (this.isStraight(cards)) return CardPattern.STRAIGHT;
                if (this.isAirplane(cards)) return CardPattern.AIRPLANE;
                return CardPattern.INVALID;
            case 8:
                if (this.isFourWithTwoPairs(cards)) return CardPattern.FOUR_WITH_TWO_PAIRS;
                if (this.isStraightPair(cards)) return CardPattern.STRAIGHT_PAIR;
                if (this.isAirplane(cards)) return CardPattern.AIRPLANE;
                return CardPattern.INVALID;
            default:
                if (this.isStraight(cards)) return CardPattern.STRAIGHT;
                if (this.isStraightPair(cards)) return CardPattern.STRAIGHT_PAIR;
                if (this.isAirplane(cards)) return CardPattern.AIRPLANE;
                if (this.isAirplaneWithWings(cards)) return CardPattern.AIRPLANE_WITH_WINGS;
                return CardPattern.INVALID;
        }
    },

    // 判断是否能大过上家的牌
    canBeatLastCards(cards, lastCards) {
        const pattern = this.getPattern(cards);
        const lastPattern = this.getPattern(lastCards);
        
        // 如果是火箭，可以打任何牌
        if (pattern === CardPattern.ROCKET) return true;
        
        // 如果上家是火箭，无法大过
        if (lastPattern === CardPattern.ROCKET) return false;
        
        // 如果是炸弹，可以打任何非火箭的牌
        if (pattern === CardPattern.BOMB) {
            if (lastPattern === CardPattern.BOMB) {
                return this.getMaxWeight(cards) > this.getMaxWeight(lastCards);
            }
            return true;
        }
        
        // 如果上家是炸弹，且自己不是炸弹或火箭，无法大过
        if (lastPattern === CardPattern.BOMB && pattern !== CardPattern.ROCKET) return false;
        
        // 其他情况必须是相同牌型，且权重更大
        if (pattern !== lastPattern) return false;
        
        // 对于特殊牌型，需要比较主体牌的权重
        switch (pattern) {
            case CardPattern.TRIPLE_WITH_ONE:
            case CardPattern.TRIPLE_WITH_PAIR:
            case CardPattern.AIRPLANE_WITH_WINGS:
                return this.getMaxWeightInTriples(cards) > this.getMaxWeightInTriples(lastCards);
            case CardPattern.FOUR_WITH_TWO:
            case CardPattern.FOUR_WITH_TWO_PAIRS:
                return this.getMaxWeightInFour(cards) > this.getMaxWeightInFour(lastCards);
            default:
                return this.getMaxWeight(cards) > this.getMaxWeight(lastCards);
        }
    },

    // 获取一组牌中的最大权重
    getMaxWeight(cards) {
        return Math.max(...cards.map(card => this.getCardWeight(card.rank)));
    },

    // 牌型判断函数
    isRocket(cards) {
        return cards.length === 2 && 
               cards.every(card => card.suit === 'JOKER') &&
               cards.some(card => card.rank === 'SMALL') &&
               cards.some(card => card.rank === 'BIG');
    },

    isBomb(cards) {
        return cards.length === 4 &&
               cards.every(card => card.rank === cards[0].rank);
    },

    isPair(cards) {
        return cards.length === 2 &&
               cards[0].rank === cards[1].rank;
    },

    isTriple(cards) {
        return cards.length === 3 &&
               cards.every(card => card.rank === cards[0].rank);
    },

    isTripleWithOne(cards) {
        if (cards.length !== 4) return false;
        const rankGroups = {};
        cards.forEach(card => {
            rankGroups[card.rank] = (rankGroups[card.rank] || []).concat(card);
        });
        return Object.values(rankGroups).some(group => group.length === 3);
    },

    isTripleWithPair(cards) {
        if (cards.length !== 5) return false;
        const rankGroups = {};
        cards.forEach(card => {
            rankGroups[card.rank] = (rankGroups[card.rank] || []).concat(card);
        });
        const groupSizes = Object.values(rankGroups).map(group => group.length);
        return groupSizes.includes(3) && groupSizes.includes(2);
    },

    isStraight(cards) {
        if (cards.length < 5) return false;
        
        const weights = cards.map(card => this.getCardWeight(card.rank)).sort((a, b) => a - b);
        
        for (let i = 1; i < weights.length; i++) {
            if (weights[i] !== weights[i-1] + 1) {
                return false;
            }
        }
        
        return Math.max(...weights) <= 14;
    },

    isStraightPair(cards) {
        if (cards.length < 6 || cards.length % 2 !== 0) return false;
        
        const rankGroups = {};
        cards.forEach(card => {
            rankGroups[card.rank] = (rankGroups[card.rank] || []).concat(card);
        });
        
        if (!Object.values(rankGroups).every(group => group.length === 2)) {
            return false;
        }
        
        const weights = Object.values(rankGroups)
            .map(group => this.getCardWeight(group[0].rank))
            .sort((a, b) => a - b);
        
        for (let i = 1; i < weights.length; i++) {
            if (weights[i] !== weights[i-1] + 1) {
                return false;
            }
        }
        
        return Math.max(...weights) <= 14;
    },

    isAirplane(cards) {
        if (cards.length < 6 || cards.length % 3 !== 0) return false;
        
        const rankGroups = {};
        cards.forEach(card => {
            rankGroups[card.rank] = (rankGroups[card.rank] || []).concat(card);
        });
        
        const triples = Object.values(rankGroups).filter(group => group.length === 3);
        
        if (triples.length < 2) return false;
        
        const weights = triples
            .map(group => this.getCardWeight(group[0].rank))
            .sort((a, b) => a - b);
        
        for (let i = 1; i < weights.length; i++) {
            if (weights[i] !== weights[i-1] + 1) {
                return false;
            }
        }
        
        return Math.max(...weights) <= 14;
    },

    isAirplaneWithWings(cards) {
        const rankGroups = {};
        cards.forEach(card => {
            rankGroups[card.rank] = (rankGroups[card.rank] || []).concat(card);
        });
        
        const triples = Object.values(rankGroups).filter(group => group.length === 3);
        
        if (triples.length < 2) return false;
        
        const weights = triples
            .map(group => this.getCardWeight(group[0].rank))
            .sort((a, b) => a - b);
        
        for (let i = 1; i < weights.length; i++) {
            if (weights[i] !== weights[i-1] + 1) {
                return false;
            }
        }
        
        const wingsCount = cards.length - (triples.length * 3);
        if (wingsCount !== triples.length && wingsCount !== triples.length * 2) {
            return false;
        }
        
        if (wingsCount === triples.length * 2) {
            const pairs = Object.values(rankGroups).filter(group => group.length === 2);
            if (pairs.length !== triples.length) {
                return false;
            }
        }
        
        return Math.max(...weights) <= 14;
    },

    isFourWithTwo(cards) {
        if (cards.length !== 6) return false;
        
        const rankGroups = {};
        cards.forEach(card => {
            rankGroups[card.rank] = (rankGroups[card.rank] || []).concat(card);
        });
        
        // 必须有一个四张的组合
        const hasFour = Object.values(rankGroups).some(group => group.length === 4);
        
        // 剩下的两张可以是任意牌(可以相同也可以不同)
        return hasFour && cards.length === 6;
    },

    isFourWithTwoPairs(cards) {
        if (cards.length !== 8) return false;
        
        const rankGroups = {};
        cards.forEach(card => {
            rankGroups[card.rank] = (rankGroups[card.rank] || []).concat(card);
        });
        
        const hasFour = Object.values(rankGroups).some(group => group.length === 4);
        const pairCount = Object.values(rankGroups).filter(group => group.length === 2).length;
        
        return hasFour && pairCount === 2;
    },

    // 获取牌型描述
    getPatternDescription(pattern) {
        switch(pattern) {
            case CardPattern.SINGLE: return '单张';
            case CardPattern.PAIR: return '对子';
            case CardPattern.TRIPLE: return '三张';
            case CardPattern.TRIPLE_WITH_ONE: return '三带一';
            case CardPattern.TRIPLE_WITH_PAIR: return '三带对';
            case CardPattern.STRAIGHT: return '顺子';
            case CardPattern.STRAIGHT_PAIR: return '连对';
            case CardPattern.AIRPLANE: return '飞机';
            case CardPattern.AIRPLANE_WITH_WINGS: return '飞机带翅膀';
            case CardPattern.BOMB: return '炸弹';
            case CardPattern.ROCKET: return '王炸';
            case CardPattern.FOUR_WITH_TWO: return '四带二';
            case CardPattern.FOUR_WITH_TWO_PAIRS: return '四带二对';
            default: return '无效牌型';
        }
    },

    // 获取四带牌型中四张牌的权重
    getMaxWeightInFour(cards) {
        const rankGroups = {};
        cards.forEach(card => {
            rankGroups[card.rank] = (rankGroups[card.rank] || []).concat(card);
        });
        
        let maxWeight = 0;
        Object.entries(rankGroups).forEach(([rank, group]) => {
            if (group.length === 4) {
                maxWeight = this.getCardWeight(rank);
            }
        });
        
        return maxWeight;
    },

    // 获取三带牌型中三张牌的权重
    getMaxWeightInTriples(cards) {
        const rankGroups = {};
        cards.forEach(card => {
            rankGroups[card.rank] = (rankGroups[card.rank] || []).concat(card);
        });
        
        let maxWeight = 0;
        Object.entries(rankGroups).forEach(([rank, group]) => {
            if (group.length === 3) {
                const weight = this.getCardWeight(rank);
                maxWeight = Math.max(maxWeight, weight);
            }
        });
        
        return maxWeight;
    }
}; 